using UnityEngine;
using System.Collections.Generic;

/// <summary>
/// 
/// </summary>
public class DataManager : MonoBehaviour
{
    #region Static Data

    static public DataManager Instance;

    #endregion

    #region Public Data

    public Texture2D[] textures;
    public Font[] fonts;
    public AudioClip[] audios;

    [HideInInspector]
    public Texture2D textureClear;

    #endregion

    #region Private Data

    private Dictionary<string, Texture2D> _dictTextures;
    private Dictionary<string, Font> _dictFonts;
    private Dictionary<string, AudioClip> _dictAudios;

    #endregion

    /// <summary>
    /// 
    /// </summary>
    void Awake()
    {
        Instance = this;

        _dictTextures = new Dictionary<string, Texture2D>();
        for (int i = 0; i < textures.Length; i++)
        {
            if (textures[i] == null) continue;
            if (_dictTextures.ContainsKey(textures[i].name)) continue;

            _dictTextures.Add(textures[i].name, textures[i]);
        }

        _dictFonts = new Dictionary<string, Font>();
        for (int i = 0; i < fonts.Length; i++)
        {
            if (fonts[i] == null) return;
            if (_dictFonts.ContainsKey(fonts[i].name)) continue;

            _dictFonts.Add(fonts[i].name, fonts[i]);
        }

        _dictAudios = new Dictionary<string, AudioClip>();
        for (int i = 0; i < audios.Length; i++)
        {
            if (audios[i] == null) return;
            if (_dictAudios.ContainsKey(audios[i].name)) continue;

            _dictAudios.Add(audios[i].name, audios[i]);
        }

        //--> Create clear color.
        Color[] colorClear = new Color[4];
        for(int i = 0; i < colorClear.Length; i++)
        {
            colorClear[i] = new Color(1f, 1f, 1f, 0f);   
        }
        textureClear = new Texture2D(2, 2);
        textureClear.SetPixels(colorClear);
        textureClear.Apply();
    }


    /// <summary>
    /// 
    /// </summary>
    public Texture2D GetTexture(string p_name)
    {
        if (!_dictTextures.ContainsKey(p_name))
        {
            Debug.LogError("Don't have texture: " + p_name);
            return new Texture2D(2, 2);
        }

        return _dictTextures[p_name];
    }

    /// <summary>
    /// 
    /// </summary>
    public Font GetFont(string p_name)
    {
        if (!_dictFonts.ContainsKey(p_name))
        {
            Debug.LogError("Don't have font: " + p_name);
            return new Font();
        }

        return _dictFonts[p_name];
    }

    /// <summary>
    /// 
    /// </summary>
    public AudioClip GetAudio(string p_name)
    {
        if (!_dictAudios.ContainsKey(p_name))
        {
            Debug.LogError("Don't have audio: " + p_name);
            return new AudioClip();
        }

        return _dictAudios[p_name];
    }
}
